﻿using Benlai.Util;
using Benlai.Model;
using Benlai.Model.Doc;
using Benlai.Bll.Doc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace BenlaiStudy.Controllers
{
    public class DocController : BaseController
    {
        //
        // GET: /Doc/
        public ActionResult Index()
        {
            ViewBag.MenuNo = 10020002;
            return View();
        }

        //
        // GET: /Doc/DocModify
        public ActionResult DocModify()
        {
            return View();
        }

        // POST: /Doc/SetTable
        [HttpPost]
        public ActionResult SetTable(TableModel model)
        {
            try
            {
                DocManager doc = new DocManager();
                doc.SetTable(model);
                return Json(ResultInfo<int>.GetSuccessResultInfo(0, "ok"));
            }
            catch (Exception ex)
            {
                return Json(ResultInfo<int>.GetFailureResultInfo(ex.Message));
            }
        }
        // GET: /Doc/GetTable
        [HttpGet]
        public ActionResult GetTable(string id)
        {
            try
            {
                DocManager doc = new DocManager();
                var model = doc.GetTable(id);
                return Json(ResultInfo<TableModel>.GetSuccessResultInfo(model, "ok"));
            }
            catch (Exception ex)
            {
                return Json(ResultInfo<TableModel>.GetFailureResultInfo(ex.Message));
            }
        }
        // GET: /Doc/GetTableListByTable
        [HttpGet]
        public ActionResult GetTableListByTable(string id)
        {
            try
            {
                string tableName = id;
                DocManager doc = new DocManager();
                var list = doc.GetTableListByTable(tableName);
                return Json(ResultInfo<List<TableModel>>.GetSuccessResultInfo(list, "ok"), JsonRequestBehavior.AllowGet);
            }
            catch (Exception ex)
            {
                return Json(ResultInfo<int>.GetFailureResultInfo(ex.Message), JsonRequestBehavior.AllowGet);
            }
        }
        // GET: /Doc/GetTableListByColumn
        [HttpGet]
        public ActionResult GetTableListByColumn(string id)
        {
            try
            {
                string columnName = id;
                DocManager doc = new DocManager();
                var list = doc.GetTableListByColumn(columnName);
                return Json(ResultInfo<List<TableModel>>.GetSuccessResultInfo(list, "ok"), JsonRequestBehavior.AllowGet);
            }
            catch (Exception ex)
            {
                return Json(ResultInfo<int>.GetFailureResultInfo(ex.Message), JsonRequestBehavior.AllowGet);
            }
        }


        public ActionResult InterfaceDoc()
        {
            return View();
        }



        // GET: /Doc/GetInterfaceDoc/{id}
        [HttpGet]
        public ActionResult GetInterfaceDoc(string id)
        {
            try
            {
                InterfaceDocManager doc = new InterfaceDocManager();
                InterfaceDocModel model = doc.Get(id);
                return Json(ResultInfo<InterfaceDocModel>.GetSuccessResultInfo(model, "ok"), JsonRequestBehavior.AllowGet);
            }
            catch (Exception ex)
            {
                return Json(ResultInfo<InterfaceDocModel>.GetFailureResultInfo(ex.Message), JsonRequestBehavior.AllowGet);
            }
        }

        // GET: /Doc/GetInterfaceDocList
        [HttpGet]
        public ActionResult GetInterfaceDocList()
        {
            try
            {
                InterfaceDocManager doc = new InterfaceDocManager();
                var model = doc.GetList();
                return Json(ResultInfo<List<InterfaceDocModel>>.GetSuccessResultInfo(model, "ok"), JsonRequestBehavior.AllowGet);
            }
            catch (Exception ex)
            {
                return Json(ResultInfo<List<InterfaceDocModel>>.GetFailureResultInfo(ex.Message), JsonRequestBehavior.AllowGet);
            }
        }

        // POST: /Doc/SaveInterfaceDoc
        [HttpPost]
        public ActionResult SaveInterfaceDoc(InterfaceDocModel model)
        {
            try
            {
                InterfaceDocManager doc = new InterfaceDocManager();

                InterfaceDocModel model1 = doc.Get(model.No);
                if (model1 != null)
                    model.Id = model1.Id;

                doc.Set(model);
                return Json(ResultInfo<int>.GetSuccessResultInfo(0, "ok"));
            }
            catch (Exception ex)
            {
                return Json(ResultInfo<int>.GetFailureResultInfo(ex.Message));
            }
        }

        // POST: /Doc/SaveInterfaceDoc
        [HttpPost]
        public ActionResult Publish(string no, string html)
        {
            try
            {
                string htmlcontent = "<html><head><meta charset=\"utf-8\"><title>API</title><link rel=\"stylesheet\" href=\"/Content/noke/css/bootstrap.min.css\" /><link rel=\"stylesheet\" href=\"/Content/noke/js/plugins/editormd/editormd.css\" /></head><body>";
                htmlcontent += "<section class=\"container features\"><div class=\"markdown-body editormd-preview-container\" previewcontainer=\"true\" style=\"padding: 20px;\">" + HttpContext.Server.UrlDecode(html) + "</div></section></body></html>";

                InterfaceDocManager doc = new InterfaceDocManager();

                string fileName = System.Web.HttpContext.Current.Server.MapPath("~/") + @"Content\Document\Api\" + no + ".html";
                doc.Publish(fileName, htmlcontent);
                
                return Json(ResultInfo<int>.GetSuccessResultInfo(0, "ok"));
            }
            catch (Exception ex)
            {
                return Json(ResultInfo<int>.GetFailureResultInfo(ex.Message));
            }
        }
    }
}
